package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.mapper.AddressBookMapper;
import com.graduation.springbootinit.model.entity.ShopPart.AddressBook;
import com.graduation.springbootinit.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Resource
    private AddressBookMapper addressBookMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Void> addAddress(AddressBook addressBook) {
        // 检查是否存在相同地址
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", addressBook.getUserId())
                   .eq("province_code", addressBook.getProvinceCode())
                   .eq("city_code", addressBook.getCityCode())
                   .eq("district_code", addressBook.getDistrictCode())
                   .eq("detail", addressBook.getDetail());
        
        // 查询是否已存在相同地址
        AddressBook existAddress = addressBookMapper.selectOne(queryWrapper);
        if (existAddress != null) {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR.getCode(), "该地址已存在");
        }
        
        // 如果是默认地址，先将其他地址设为非默认
        if (addressBook.getIsDefault() == 1) {
            addressBookMapper.removeDefaultStatus(addressBook.getUserId());
        }
        addressBookMapper.insert(addressBook);
        return ResultUtils.success(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefault(Long id, Long userId) {
        // 先将该用户的所有地址设为非默认
        addressBookMapper.removeDefaultStatus(userId);
        // 将指定地址设为默认
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBook.setIsDefault(1);
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public AddressBook getDefault(Long userId) {
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .eq("is_default", 1);
        return addressBookMapper.selectOne(queryWrapper);
    }

    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.selectById(id);
    }

    @Override
    public List<AddressBook> listByUserId(Long userId) {
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .orderByDesc("is_default");
        return addressBookMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddress(AddressBook addressBook) {
        // 如果设置为默认地址，先将其他地址设为非默认
        if (addressBook.getIsDefault() == 1) {
            addressBookMapper.removeDefaultStatus(addressBook.getUserId());
        }
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public void deleteAddress(Long id) {
        addressBookMapper.deleteById(id);
    }
} 